﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace JNCY.Win
{
   public class FileHelper
    {
        public static void Log(string txt)
        {
            string pathdir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorChar + "";

            string path = Path.Combine(pathdir, "exc");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            string paths = Path.Combine(path, string.Format("{0}.txt",DateTime.Now.ToString("yyyyMMdd")));

            //if (!File.Exists(paths))
            //{
            //    File.WriteAllText(paths,string.Format("{0}---{1}{2}", DateTime.Now.ToString("yy.MM.dd HH:mm:ss"), txt, Environment.NewLine));
            //}
            //else
            //{
            //    File.AppendAllText(paths, string.Format("{0}---{1}{2}", DateTime.Now.ToString("yy.MM.dd HH:mm:ss"), txt, Environment.NewLine));
            //}

            using (FileStream fs = new FileStream(paths, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
            {
                if (fs.CanWrite)
                {
                    using (StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default))
                    {
                        sw.WriteLine(string.Format("{0}---{1}{2}", DateTime.Now.ToString("yy.MM.dd HH:mm:ss"), txt, Environment.NewLine));
                        sw.Flush();
                    }
                }
            }

        }

    }
}
